using Coding4Fun.Phone.Controls;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.UI.Util;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class SnsInputerCtrl : UserControl
	{
		private const string TAG = "SnsInputerCtrl";
		private const int OldSmileyHeight = 342;
		private bool mBIsFirstLoaded = true;
		private double mSipHeight;
		private bool isSmileyIconShow = true;
		public ChatInputMode mCuInputMode;
		private bool mIsDelayShowSmiley;
		private bool isInputFocus;
		internal Grid btnLayoutRoot;
		internal StackPanel FootArea;
		internal StackPanel inputBk;
		internal UserHeadCtrl inputerUserHead;
		internal Grid inputerPanel;
		internal ChatInputTextBox inputTxt;
		internal Grid gridInputBoxCover;
		internal RoundButton SmileySendBtn;
		internal RoundButton btnLike;
		internal StackPanel SmileySpace;
		internal SmileyOverlay SmileyView;
		private bool _contentLoaded;
		public event MMUIEvent mDlgOnInputerGotFocus;
		public event MMUIEvent mDlgOnInputerLostFocus;
		public event MMUIEvent mDlgOnInputerHeightChange;
		public event MMUIEvent mDlgOnInputerSend;
		public event MMUIEvent mDlgOnInputerModeChange;
		public event MMUIEvent mDlgBtnLikeClick;
		public SnsInputerCtrl()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
			this.inputTxt.add_GotFocus(new RoutedEventHandler(this.inputTxt_GotFocus));
			this.inputTxt.add_LostFocus(new RoutedEventHandler(this.inputTxt_LostFocus));
			this.InitSmiley();
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		public void SetHeadUser(string strUserName)
		{
			this.inputerUserHead.ItemObject = strUserName;
		}
		public void SetInputerKind(SnsInputerKind kind)
		{
			if (kind == SnsInputerKind.Kind_HaveLikeBtn)
			{
				this.inputTxt.set_Width(258.0);
				this.inputerPanel.set_Width(267.0);
				this.btnLike.set_Visibility(0);
				return;
			}
			if (kind == SnsInputerKind.Kind_WithoutLikeBtn)
			{
				this.inputTxt.set_Width(329.0);
				this.inputerPanel.set_Width(338.0);
				this.btnLike.set_Visibility(1);
			}
		}
		public void FocusInputer()
		{
			if (this.mCuInputMode == ChatInputMode.InputTxtMode)
			{
				base.get_Dispatcher().BeginInvoke(delegate
				{
					if (this.inputTxt.realBox != null)
					{
						this.inputTxt.FocusRealBox();
					}
				});
			}
		}
		public void SipPushHeightChange(double dHeight)
		{
			this.mSipHeight = dHeight;
			if (dHeight <= 0.01 && this.mIsDelayShowSmiley)
			{
				this.SmileySpace.set_Height(342.0);
				this.SmileyView.set_Height(342.0);
				this.SmileyView.set_Visibility(0);
				this.mIsDelayShowSmiley = false;
				if (this.mDlgOnInputerHeightChange != null)
				{
					this.mDlgOnInputerHeightChange(this, this.SmileyView.get_Height());
				}
			}
		}
		public bool IsSipOpen()
		{
			return this.mSipHeight > 0.001;
		}
		public void ProcBackKeyPressed(CancelEventArgs cancelEventArgs_0)
		{
			if (this.mCuInputMode == ChatInputMode.InputSmileyMode)
			{
				this.SwitchMode(ChatInputMode.InputTxtMode);
				cancelEventArgs_0.set_Cancel(true);
			}
		}
		private void inputTxt_Tap(object sender, GestureEventArgs e)
		{
			this.SwitchMode(ChatInputMode.InputTxtMode);
			base.UpdateLayout();
			this.gridInputBoxCover.set_Visibility(1);
			base.get_Dispatcher().BeginInvoke(delegate
			{
				this.inputTxt.FocusRealBox();
			});
			Log.smethod_5("SnsInputerCtrl", "inputTxt_Tap:");
		}
		private void ShowSmileyIcon(bool showSmileyIcon)
		{
			if (showSmileyIcon == this.isSmileyIconShow)
			{
				return;
			}
			if (showSmileyIcon)
			{
				this.SmileySendBtn.ImageSource = (new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/expression.png") as ImageSource);
			}
			else
			{
				this.SmileySendBtn.ImageSource = (new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/keyboard.png") as ImageSource);
			}
			this.isSmileyIconShow = showSmileyIcon;
		}
		public void SwitchMode(ChatInputMode mode)
		{
			if (this.mCuInputMode == mode)
			{
				return;
			}
			this.mCuInputMode = mode;
			switch (mode)
			{
			case ChatInputMode.InputVoiceMode:
				break;
			case ChatInputMode.InputSmileyMode:
				this.ShowSmileyIcon(false);
				this.inputTxt.set_Visibility(0);
				if (this.IsSipOpen())
				{
					this.mIsDelayShowSmiley = true;
				}
				else
				{
					this.SmileySpace.set_Height(342.0);
					this.SmileyView.set_Height(342.0);
					this.SmileyView.set_Visibility(0);
					if (this.mDlgOnInputerHeightChange != null)
					{
						this.mDlgOnInputerHeightChange(this, this.SmileyView.get_Height());
					}
				}
				break;
			default:
				this.ShowSmileyIcon(true);
				this.inputTxt.set_Visibility(0);
				this.SmileySpace.set_Height(0.0);
				this.SmileyView.set_Height(0.0);
				this.SmileyView.set_Visibility(1);
				if (this.mDlgOnInputerHeightChange != null)
				{
					this.mDlgOnInputerHeightChange(this, 0);
				}
				break;
			}
			if (this.mDlgOnInputerModeChange != null)
			{
				this.mDlgOnInputerModeChange(this, mode);
			}
		}
		private void inputTxt_LostFocus(object sender, RoutedEventArgs e)
		{
			this.isInputFocus = false;
			if (this.mDlgOnInputerLostFocus != null)
			{
				this.mDlgOnInputerLostFocus(this, null);
			}
			this.gridInputBoxCover.set_Visibility(0);
			Log.smethod_5("SnsInputerCtrl", "inputTxt_LostFocus:");
		}
		private void inputTxt_GotFocus(object sender, RoutedEventArgs e)
		{
			if (this.mDlgOnInputerGotFocus != null)
			{
				this.mDlgOnInputerGotFocus(this, null);
			}
			Log.smethod_5("SnsInputerCtrl", "inputTxt_GotFocus:");
			this.isInputFocus = true;
			this.SwitchMode(ChatInputMode.InputTxtMode);
			this.gridInputBoxCover.set_Visibility(1);
		}
		private void btnLike_MouseLeave(object sender, MouseEventArgs e)
		{
		}
		private void btnLike_Click(object sender, RoutedEventArgs e)
		{
			if (this.mDlgBtnLikeClick != null)
			{
				this.mDlgBtnLikeClick(this, null);
			}
		}
		private void SmileySendBtn_MouseLeave(object sender, MouseEventArgs e)
		{
			if (!this.IsSipOpen())
			{
				return;
			}
			Log.smethod_5("SnsInputerCtrl", "SmileySendBtn_MouseLeave");
			this.DoSmileySendBtnEvent();
		}
		private void SmileySendBtn_Click(object sender, RoutedEventArgs e)
		{
			if (this.IsSipOpen())
			{
				return;
			}
			Log.smethod_5("SnsInputerCtrl", "SmileySendBtn_Click");
			this.DoSmileySendBtnEvent();
		}
		private void DoSmileySendBtnEvent()
		{
			this.inputTxt.set_IsEnabled(false);
			this.inputTxt.set_IsEnabled(true);
			if (this.mCuInputMode == ChatInputMode.InputSmileyMode)
			{
				this.SwitchMode(ChatInputMode.InputTxtMode);
				if (this.inputTxt == null || this.inputTxt.realBox == null)
				{
					return;
				}
				try
				{
					this.inputTxt.FocusRealBox();
					return;
				}
				catch (Exception)
				{
					return;
				}
			}
			this.SwitchMode(ChatInputMode.InputSmileyMode);
		}
		private void inputTxt_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.get_Key() == 3)
			{
				this.DoSend();
				e.set_Handled(true);
			}
		}
		private void DoSend()
		{
			string text = this.inputTxt.Text;
			if (!string.IsNullOrWhiteSpace(text))
			{
				this.inputTxt.Text = "";
				this.SmileyView.ClearSmileyStack();
				bool flag = false;
				bool flag2 = false;
				string text2 = EmojiManager.GetInstance().ConvertInputStrToCodeStr(text, ref flag);
				text2 = EmojiManager.GetInstance().SbCodeToUtf16Code(text2, ref flag2);
				if (this.mDlgOnInputerSend != null)
				{
					this.mDlgOnInputerSend(this, text2);
					return;
				}
			}
			else
			{
				this.mDlgOnInputerSend(this, "");
				MessageBoxManager.ShowToast(strings.ChatEmptyTxtHint, 2500, null, false);
			}
		}
		private void SmileyIconButton_Click(object sender, EventArgs e)
		{
			Log.smethod_5("SnsInputerCtrl", "SmileyIconButton_Click");
			this.inputTxt.set_IsEnabled(false);
			this.inputTxt.set_IsEnabled(true);
			if (this.mCuInputMode == ChatInputMode.InputSmileyMode)
			{
				this.SwitchMode(ChatInputMode.InputTxtMode);
				return;
			}
			this.SwitchMode(ChatInputMode.InputSmileyMode);
		}
		public void EmojiSelectComplete(string EmojiStr)
		{
			string.IsNullOrEmpty(EmojiStr);
		}
		private void InitSmiley()
		{
			this.SmileyView.Click = new SmileyClick(this.SmileyClick);
			this.SmileyView.EmojiClick = new SmileyClick(this.EmojiClick);
		}
		private void SmileyClick(int smiley)
		{
			if (smiley == -1)
			{
				this.DoSend();
				return;
			}
			if (smiley != -2)
			{
				int selectionStart = this.inputTxt.realBox.get_SelectionStart();
				string text = this.inputTxt.Text;
				string text2 = text.Substring(0, selectionStart);
				string smileyText = SmileyManager.GetInstance().getSmileyText(smiley);
				this.SmileyView.PushSmileyStack(smileyText);
				string text3 = text2 + smileyText;
				int length = text3.get_Length();
				text3 += text.Substring(selectionStart);
				this.inputTxt.Text = text3;
				this.inputTxt.realBox.set_SelectionStart(length);
				this.inputTxt.realBox.set_SelectionLength(0);
				return;
			}
			if (this.inputTxt.realBox.get_SelectionLength() > 0)
			{
				int selectionStart2 = this.inputTxt.realBox.get_SelectionStart();
				int selectionLength = this.inputTxt.realBox.get_SelectionLength();
				string text4 = this.inputTxt.Text;
				string text5 = text4.Substring(0, selectionStart2) + text4.Substring(selectionStart2 + selectionLength);
				this.inputTxt.Text = text5;
				this.inputTxt.realBox.set_SelectionStart(selectionStart2);
				this.inputTxt.realBox.set_SelectionLength(0);
				return;
			}
			int selectionStart3 = this.inputTxt.realBox.get_SelectionStart();
			string text6 = this.inputTxt.Text;
			string text7 = text6.Substring(0, selectionStart3);
			string text8 = this.SmileyView.RemoveLastSmiley(text7);
			int num = text7.get_Length() - text8.get_Length();
			text7 = text8 + text6.Substring(selectionStart3);
			this.inputTxt.Text = text7;
			this.inputTxt.realBox.set_SelectionStart(selectionStart3 - num);
			this.inputTxt.realBox.set_SelectionLength(0);
		}
		private void EmojiClick(int emojiIndex)
		{
			int selectionStart = this.inputTxt.realBox.get_SelectionStart();
			string text = this.inputTxt.Text;
			string text2 = text.Substring(0, selectionStart);
			string utf16CodeByIndex = EmojiManager.GetInstance().GetUtf16CodeByIndex(emojiIndex);
			this.SmileyView.PushSmileyStack(utf16CodeByIndex);
			string text3 = text2 + utf16CodeByIndex;
			int length = text3.get_Length();
			text3 += text.Substring(selectionStart);
			this.inputTxt.Text = text3;
			this.inputTxt.realBox.set_SelectionStart(length);
			this.inputTxt.realBox.set_SelectionLength(0);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SnsInputerCtrl.xaml", 2));
			this.btnLayoutRoot = (Grid)base.FindName("btnLayoutRoot");
			this.FootArea = (StackPanel)base.FindName("FootArea");
			this.inputBk = (StackPanel)base.FindName("inputBk");
			this.inputerUserHead = (UserHeadCtrl)base.FindName("inputerUserHead");
			this.inputerPanel = (Grid)base.FindName("inputerPanel");
			this.inputTxt = (ChatInputTextBox)base.FindName("inputTxt");
			this.gridInputBoxCover = (Grid)base.FindName("gridInputBoxCover");
			this.SmileySendBtn = (RoundButton)base.FindName("SmileySendBtn");
			this.btnLike = (RoundButton)base.FindName("btnLike");
			this.SmileySpace = (StackPanel)base.FindName("SmileySpace");
			this.SmileyView = (SmileyOverlay)base.FindName("SmileyView");
		}
	}
}
